package com.lkm.market2_0.m_imple.steelyardserver;

import com.lkm.frame.P;
import com.lkm.frame.task2.Task;
import com.lkm.frame.task2.TaskAdapter;
import com.lkm.hardware_connect.IHardwareControler;
import com.lkm.market2_0.ExpandConfig;
import com.lkm.market2_0.help.RandomStringUtils;
import com.lkm.market2_0.help.UIP;
import com.lkm.market2_0.m_imple.steelyardserver.server.Config;
import com.lkm.market2_0.task.DefaultTaskExecutor;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;



 abstract class NetCom {

    public void connect() {
        startServer();
    }

    
    private void startServer(){
        Task start=new TaskAdapter(new DefaultTaskExecutor(true)
                ,"", ExpandConfig.instance.getTaskManager2()){
            @Override
            public void doExecuting(Object param) {
                super.doExecuting(param);
                mTaskExecutor.executorDoAsyncExecuting(new Runnable() {
                    @Override
                    public void run() {
                        NetCom.this.runServer();
                    }
                });

            }
        };

        start.action();
        start.execTask();
    }

    ServerSocket server = null;
    private void runServer() {

        while (true) {
            try {
                server = new ServerSocket(ExpandConfig.instance.steelyardServerPort());
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(server==null){

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else{
                break;
            }
        }

        P.p("==== ");
        while (true) {
            try {
                
                P.p("==== 接待 -=");
                Socket socket = server.accept();

                try{
                    socket.setSoTimeout(Config.steelyardActionOutTime);
                }catch (Exception e){

                }
                socket.setKeepAlive(true);

                try {
                    
                    InetAddress inetAddress = socket.getInetAddress();

                    

                    String clientSessionId=inetAddress.getHostAddress()+socket.getPort()+ RandomStringUtils.random(6,RandomStringUtils.azAZ90);


//                        steelyardMac=inetAddress.getHostAddress()+socket.getPort();

//                        steelyardMac=NetHelp.findLANCommunicationMacByIpForLinux(inetAddress.getHostAddress());


                    if(P.isDebug()) {
                        P.p("====ip mac " + inetAddress.getHostAddress() + "   " + clientSessionId);
                        if(UIP.isPrint()){
                            UIP.p("接待新机： session：" + clientSessionId+"  IP地址："+inetAddress.getHostAddress() ,clientSessionId);
                        }
                    }


                    Task<IHardwareControler, ?, ?> readTask = createReadTask(clientSessionId, new com.lkm.market2_0.m_imple.steelyardserver.ScoketHardwareControler(socket));
                    readTask.action();
                    readTask.execTask();

                } catch (Exception e) {

                    e.printStackTrace();
                }

            } catch (IOException e) {
            }
        }

    }

    

    protected abstract Task<IHardwareControler, ?, ?>  createReadTask(String clientSessionId, IHardwareControler hardwareControler);

}
